package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.SetmealDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.mapper.SetmealDishMapper;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.awt.peer.LabelPeer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    private String name;

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize){
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();

        if (name != null && !name.equals("")){
            wrapper.like("name",name);

        }

        setmealService.page(pageInfo,wrapper);

        //新建一个dto对象
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<SetmealDto> dtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo.getRecords()){
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,dto);
            Category category = categoryService.getById(setmeal.getCategoryId());

            if (category != null){
                //分类名称
                dto.setCategoryName(category.getName());
            }
            dtoList.add(dto);
        }
        //重新设置集合
        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);
    }

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto dto){

        setmealService.saveSetmeal(dto);
        return R.success("添加成功");
    }

    //根据id获取套餐信息
    @GetMapping("/{setmealId}")
    public R<SetmealDto> getById(@PathVariable("setmealId") Long setmealId){
        //根据id获取对象
        Setmeal setmeal = setmealService.getById(setmealId);
        //新建dto对象
        SetmealDto dto = new SetmealDto();
        //属性赋值
        BeanUtils.copyProperties(setmeal,dto);
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealId);

        //条件查询
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);

        //将查询到的关联的关系列表 设置给dto
        dto.setSetmealDishes(setmealDishList);

        //返回dto
        return R.success(dto);
    }

    //修改套餐
    @PutMapping
    public R<String> update(@RequestBody SetmealDto dto){
        setmealService.updateSetmeal(dto);
        return R.success("修改成功");
    }

    //删除套餐
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,Long[] ids){
        List<Setmeal> setmealList = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmealList) {
            setmeal.setStatus(status);
        }
        //批量修改
        setmealService.updateBatchById(setmealList);
        return R.success("修改成功");
    }



    //根据分类id查询套餐列表
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId){
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.eq("status",1);
        //查询
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }


    //根据套餐id 查询套餐关联的菜品
    @GetMapping("/dish/{setmealId}")
    public R<List<SetmealDish>> getSetMeal(@PathVariable("setmealId") Long setmealId){
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealId);
        List<SetmealDish> dishList = setmealDishService.list(wrapper);
        return R.success(dishList);
    }
}
